<?php
namespace jy_lineks;
defined('BY_JYA') or exit('error');
/*
	分润计算	
*/
class commission extends \plugin{
	/*
		微客分销
	*/
	function jy_commission($order,$goodstitle=''){
		$basic=$this->plugin_setting('basic');
		$jy_commission=intval($basic['open_sale']);
		if(!$jy_commission){
			return;
		}
		$order['price']=$order['fee'];
		$order['goods'][]=array('title'=>$goodstitle?:'开通VIP');
		model('jy_commission/commission')->create($order);
		return true;
	}
	/*
		团队极差分红
	*/
	function jy_teamfee($order){
		$jy_teamfee=intval($this->setting['jy_teamfee']);
		if(!$jy_teamfee){
			return;
		}
		$order['price']=$order['fee'];
		$order['goods'][]=array('title'=>$goodstitle?:'开通VIP');
		model('jy_teamfee/commission')->create($order);
		return true;
	}
	/*
		区域代理分红
	*/
	function jy_area_agent($order,$goodstitle=''){
		$jy_area_agent=intval($this->setting['jy_area_agent']);
		if(!$jy_area_agent){
			return;
		}
		$address=pdo_getcolumn('jy_ailiao_member',array('mid'=>$order['mid']),'pcd');
		$address=json_decode($address,true);
		if(!$address){
			return;
		}
		$pluginset=plugin('jy_area_agent')->setting;
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$order['mid'],
			'ordersn'=>$order['ordersn'],
			'order_price'=>$order['money'],
			'commission_money'=>0,
			'xuni_money'=>0,
			'createtime'=>TIMESTAMP,
		);
		//佣金计算方式，是否扣除运费
		if($this->setting['commissiontype']==1){
			$this->commission_order['commission_price']=$order['price'];
		}else{
			$this->commission_order['commission_price']=$order['price']-$order['dispatchprice'];
		}
		plugin('jy_area_agent')->commission_order=$this->commission_order;
		if($pluginset['open_duodai']){
			//计算多省代
			model('jy_area_agent/commission')->duodai_commission($address);
		}
		//计算省级代理分红
		$this->province_commission($address);
		if($pluginset['level']>1){
			//计算市级代理分红
			model('jy_area_agent/commission')->city_commission($address);
		}
		if($pluginset['level']>2){
			//计算县级代理分红
			model('jy_area_agent/commission')->district_commission($address);
		}
		if($pluginset['level']>3){
			//计算镇级代理分红
			model('jy_area_agent/commission')->street_commission($address);
		}
		//生成分销订单
		if($this->commission_order['isarea']){
			$indata=$this->commission_order;
			unset($indata['isarea']);
			pdo_insert('jy_area_agent_order',$indata);
		}
		return true;
	}
}